import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Create extends JFrame implements ActionListener{
	private JLabel lbl1 = new JLabel("Full Name: ");
	private JTextField txt1 = new JTextField();
	private JLabel lbl2 = new JLabel("ID Number: ");
	private JTextField txt2 = new JTextField();
	private JLabel lbl3 = new JLabel("ClassCode: ");
	private JTextField txt3 = new JTextField();
	private JLabel lbl6 = new JLabel("User Name: ");
	private JTextField txt6 = new JTextField();
	private JLabel lbl4 = new JLabel("Password: ");
	private JPasswordField txt4 = new JPasswordField();
	private JLabel lbl5 = new JLabel("Re-Enter Password: ");
	private JPasswordField txt5 = new JPasswordField();
	private JButton btn1 = new JButton("Save Instructor");
	private JButton btn2 = new JButton("Clear All Fields");
	private JButton btn3 = new JButton("Back to Login");
	String line;
    String username = "";
    JFrame f;


public Create(){
	f = new JFrame("Add Instructor");
	f.setBounds(500,300,500,300);
	f.setLayout(new GridLayout(8,1));
	f.setVisible(true);
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	JPanel p = new JPanel();
	p.setLayout(new GridLayout(1,1));
	p.add(lbl1);
	p.add(txt1);

	JPanel p2 = new JPanel();
	p2.setLayout(new GridLayout(1,1));
	p2.add(lbl2);
	p2.add(txt2);

	JPanel p3 = new JPanel();
	p3.setLayout(new GridLayout(1,1));
	p3.add(lbl3);
	p3.add(txt3);

	JPanel p7 = new JPanel();
	p7.setLayout(new GridLayout(1,1));
	p7.add(lbl6);
	p7.add(txt6);

	JPanel p4 = new JPanel();
	p4.setLayout(new GridLayout(1,1));
	p4.add(lbl4);
	p4.add(txt4);

	JPanel p5 = new JPanel();
	p5.setLayout(new GridLayout(1,1));
	p5.add(lbl5);
	p5.add(txt5);

	JPanel p6 = new JPanel();
	p6.setLayout(new GridLayout(1,1));
	p6.add(btn1);
	p6.add(btn2);
	btn1.addActionListener(this);
	btn2.addActionListener(this);

	JPanel p8 = new JPanel();
	p8.setLayout(new GridLayout(1,1));
	p8.add(btn3);
	btn3.addActionListener(this);

		f.add(p);
		f.add(p2);
		f.add(p3);
		f.add(p7);
		f.add(p4);
		f.add(p5);
		f.add(p6);
		f.add(p8);
}
public void AddRecord(){
		ArrayList list = new ArrayList();
	try{
		BufferedReader reader = new BufferedReader(new FileReader("InstructorRecords.txt"));
		String tmp;
        while ((tmp = reader.readLine()) != null)
       	 	list.add(tmp);
        	reader.close();
			list.add(txt1.getText()+ ',' + txt2.getText() + ',' + txt3.getText() +','+ txt6.getText()+ ','+ txt4.getText()+ ',' + txt5.getText());
         //List getTxt();
			BufferedWriter writer = new BufferedWriter(new FileWriter("InstructorRecords.txt"));
            	for (int i = 0; i < list.size(); i++)
                	writer.write(list.get(i) + "\r\n");
					writer.close();

		JOptionPane.showMessageDialog(null,"New Instructor Inserted:"+ txt6.getText());

	}catch(Exception e){

	}
}
public void actionPerformed(ActionEvent e){
	Object src = e.getSource();
	String t1 = txt1.getText();
	String t2 = txt2.getText();
	String t3 = txt3.getText();
	String t4 = txt4.getText();
	String t5 = txt5.getText();
	String t6 = txt6.getText();
   /*List t1 = txt1. getText();
	 *List t2 = txt2.getText();
	 *List t3 = txt3.getText();
	 *List t4 = txt4.getText();
	 *List t5 = txt5.getText();
	 *List t6 = txt6.getText();
	 */

	if(src == btn1){
		 if (true){
			if(t1.trim().isEmpty() || t2.trim().isEmpty() || t3.trim().isEmpty() || t4.trim().isEmpty() || t5.trim().isEmpty()|| t6.trim().isEmpty()) {
				JOptionPane.showMessageDialog(null,"Cannot add without complete data!");
			}
			try {
		       	BufferedReader br = new BufferedReader(new FileReader("InstructorRecords.txt"));
		        while ( ( line = br.readLine() ) != null ) {
		        	String [] tmp = line.split(",");
		            username = tmp[3];
					if(t6.trim().equalsIgnoreCase(username)){
						JOptionPane.showMessageDialog(null,"Username already exist!");
						return;
					}
				}
	        } catch (Exception err) {
	                err.printStackTrace();
	        }
	        	AddRecord();
	        	return;
		}else if(t4.trim().length() <= 5){
				JOptionPane.showMessageDialog(null,"Weak Password!");
		}else if(!t4.trim().equals(t5.trim())){
				JOptionPane.showMessageDialog(null,"Password dont Match!");
		}

	}else if (src == btn2){
		txt1.setText("");
		txt2.setText("");
		txt3.setText("");
		txt4.setText("");
		txt5.setText("");
		txt6.setText("");
	}else if(src == btn3){
        Login s = new Login();
        s.setVisible(true);
        s.setSize(270,180);
        f.dispose();
        s.setLocation(500,250);
	}
}

}